﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Temp_20190707_AForm
{
    public partial class DockTipForm : Form
    {
        public DockTipForm()
        {
            InitializeComponent();
        }

        public static DockTipForm ShowDLPanel(DockPanel dockPanel)
        {
            var rect = dockPanel.RectangleToScreen(new Rectangle(Point.Empty, dockPanel.Size));


            
            DockTipForm form = new DockTipForm();
            form.DLPanel = new DockTipPanel(dockPanel);
            form.DLPanel.Dock = DockStyle.Fill;

            form.TagForm = dockPanel.FindForm();
            form.TagPanel = dockPanel;
            form.TopMost = true;
            form.Controls.Add(form.DLPanel);
            form.Size = new Size(0, 0);
            form.Show(form.TagForm);
            form.Location = rect.Location;
            form.Size = rect.Size;
            return form;
        }

        private void ResetRect(object sender, EventArgs e)
        {
            if (TagPanel == null) { timer1.Enabled = false; return; }
            var rect = TagPanel.RectangleToScreen(new Rectangle(Point.Empty, TagPanel.Size));
            this.Location = rect.Location;
            this.Size = rect.Size;
        }


        public DockTipPanel DLPanel { get; private set; }
        public DockPanel TagPanel { get; private set; }
        public Form TagForm { get; private set; }

        private void Form2_Load(object sender, EventArgs e)
        {
            if (TagForm != null) TagForm.LocationChanged += ResetRect;
            if (TagPanel != null)
            {
                TagPanel.LocationChanged += ResetRect;
                TagPanel.SizeChanged += ResetRect;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            Try(() => { if (TagForm != null) TagForm.LocationChanged -= ResetRect; });
            if (TagPanel != null)
            {
                Try(() => { TagPanel.LocationChanged -= ResetRect; });
                Try(() => { TagPanel.SizeChanged -= ResetRect; });
            }

            TagPanel = null;
            TagForm = null;
            Try(() => { timer1.Enabled = false; timer1.Dispose(); timer1 = null; });
        }


        private void timer1_Tick(object sender, EventArgs e)
        {

        }


        public DockEventArgs NewDockEventArgs()
        {
            return DLPanel.NewDockEventArgs();
        }



        public static void Try(Action action)
        {
            try { action(); }
            catch (ThreadAbortException) { throw; }
            catch (Exception ex) { Console.WriteLine(ex); }
        }

    }
}
